package com.tengju.bff.infrastructure.export;

import com.tengju.bff.infrastructure.shared.InfraException;
import com.tengju.export.application.ExportNoticeService;
import com.tengju.support.application.robot.FeiShuRobotService;
import com.tengju.support.domain.model.robot.*;
import com.tengju.user.domain.model.user.SsoUserId;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author: 大木
 * @date: 2021/9/24 11:14 上午
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ExportFeishuServiceImpl implements ExportNoticeService {

    private final FeiShuRobotService feiShuRobotService;

    @Override
    public void notice(Long ssoUserId, String filename, Boolean flag, String text) {
        try {
            if (ssoUserId != null && ssoUserId > 0) {
                FeiShuRobotSendImMsgPostContent postContent = new FeiShuRobotSendImMsgPostContent();
                FeiShuRobotSendImMsgPostContentContent postContentContent = new FeiShuRobotSendImMsgPostContentContent();
                List<FeiShuRobotSendImMsgPostContentContentTemplate> templateList = new ArrayList<>();
                FeiShuRobotSendImMsgPostContentContentTemplateText templateText = new FeiShuRobotSendImMsgPostContentContentTemplateText();
                FeiShuRobotSendImMsgPostContentContentTemplateA a = new FeiShuRobotSendImMsgPostContentContentTemplateA();
                templateText.setText("导出提醒: \r\n文件名: " + filename +"\r\n系统通知: ");
                templateList.add(templateText);
                if (flag) {
                    if (checkUrlByString(text)){
                        a.setText("审批通过, 点击下载");
                        a.setHref(text);
                    }else {
                        a.setText("附件格式有误, 请联系管理员！");
                    }
                    templateList.add(a);
                } else {
                    FeiShuRobotSendImMsgPostContentContentTemplateText templateTextMsg
                            = new FeiShuRobotSendImMsgPostContentContentTemplateText();
                    templateTextMsg.setText(text);
                    templateList.add(templateTextMsg);
                }
                postContentContent.setContent(List.of(templateList));
                postContent.setZh_cn(postContentContent);
                feiShuRobotService.sendMsg(new SsoUserId(ssoUserId), FeiShuRobotSendMsgType.POST, postContent);
            }
        } catch (Exception e) {
            log.error("oa FeiShu sendImMsg error", e);
            throw new InfraException("oa 飞书提醒异常", e);
        }

    }

    private boolean checkUrlByString(String text) {
        String urlRegex = "https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()!@:%_\\+.~#?&\\/\\/=]*)";
        Pattern pattern = Pattern.compile(urlRegex);
        return pattern.matcher(text).matches();
    }

}
